<template>
  <el-dialog
    :title="`${row.id ? '编辑' : '添加'}广告`"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :before-close="handleCloseDialog"
    width="900px"
  >
    <el-form
      :model="dataObject"
      :rules="rules"
      ref="adminForm"
      label-width="130px"
      size="small"
    >
      <el-form-item label="广告名称：" prop="goodsName">
        <el-input v-model="dataObject.goodsName" clearable placeholder="请输入广告名称"></el-input>
      </el-form-item>
      <el-form-item label="最低投入：" prop="minPrice">
        <el-input v-model="dataObject.minPrice" type="number" placeholder="请输入最低投入" clearable></el-input>
      </el-form-item>
      <el-form-item label="最高投入：" prop="maxPrice">
        <el-input v-model="dataObject.maxPrice" type="number" placeholder="请输入最高投入" clearable></el-input>
      </el-form-item>
      <el-form-item label="广告库存：" prop="goodsStock">
        <el-input
          v-model="dataObject.goodsStock"
          type="number"
          placeholder="请输入广告库存"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="广告图片：" prop="goodsPic">
        <SingleUpload
          :data="dataObject.goodsPic"
          folder="goods"
          @on-change="
            e => {
              onChange(e, 'goodsPic');
            }
          "
        />
      </el-form-item>
      <el-form-item label="广告详细内容：" prop="goodsDesc">
        <Editor
          :data="dataObject.goodsDesc"
          folder="goods"
          @on-change="
            e => {
              onChange(e, 'goodsDesc');
            }
          "
        />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="handleConfirmDialog('adminForm')"
        size="mini"
        :loading="loading"
        >确 定</el-button
      >
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getGoodsInfo, addGoodsInfo, updateGoodsInfo } from "@/api/goods/index";
import Editor from "@/components/Editor/index.vue";
import SingleUpload from "@/components/Upload/singleUpload.vue";
import { getStorage } from "@/utils/index";
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("goods");
const defaultObject = {
  goodsName: "",
  goodsStock: "",
  minPrice: "",
  maxPrice: "",
  goodsPic: [],
  goodsDesc: ""
};

export default {
  components: {
    Editor,
    SingleUpload
  },
  data() {
    return {
      visible: true,
      loading: false,
      dataObject: Object.assign({}, defaultObject)
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules
    })
  },
  mounted() {
    this.getLoad();
  },
  methods: {
    //初始化详情
    getLoad() {
      if (this.row.id) {
        const user = getStorage("user") || {};
        getGoodsInfo({ id: this.row.id }).then(res => {
          const { data } = res;
          if (data.goodsPic) {
            data.goodsPic = [
              { url: `${user.filePreviewUrl}/${data.goodsPic}` }
            ];
          }
          this.dataObject = data;
        });
      }
    },
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          this.loading = true;
          let params = { ...this.dataObject };
          if (this.dataObject.goodsPic && this.dataObject.goodsPic.length > 0) {
            params.goodsPic = this.dataObject.goodsPic[0].filePath;
          }
          if (this.row.id) {
            await updateGoodsInfo({ ...params, id: this.row.id });
          } else {
            await addGoodsInfo(params);
          }
          this.loading = false;
          this.resetForm(formName);
          this.$message({
            message: `${this.row.id ? "新增" : "编辑"}成功`,
            type: "success",
            duration: 1000
          });
          this.$emit("handle-confirm");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    },
    // 回调组件赋值
    onChange(e, key) {
      this.dataObject[key] = e;
      this.$refs["adminForm"].validateField(key, error => {
        if (error) {
          this.$message.error(error);
        }
      });
    }
  }
};
</script>
<style scoped></style>
